package cn.iocoder.yudao.module.project.service.supplier;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.supplier.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.supplier.SupplierDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.supplier.SupplierMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 供应商信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SupplierServiceImpl implements SupplierService {

    @Resource
    private SupplierMapper supplierMapper;

    @Override
    public String createSupplier(SupplierSaveReqVO createReqVO) {
        // 插入
        SupplierDO supplier = BeanUtils.toBean(createReqVO, SupplierDO.class);
        supplierMapper.insert(supplier);
        // 返回
        return supplier.getId();
    }

    @Override
    public void updateSupplier(SupplierSaveReqVO updateReqVO) {
        // 校验存在
        validateSupplierExists(updateReqVO.getId());
        // 更新
        SupplierDO updateObj = BeanUtils.toBean(updateReqVO, SupplierDO.class);
        supplierMapper.updateById(updateObj);
    }

    @Override
    public void deleteSupplier(String id) {
        // 校验存在
        validateSupplierExists(id);
        // 删除
        supplierMapper.delete(new QueryWrapper<SupplierDO>().eq("id", id));
    }

    private void validateSupplierExists(String id) {
        if (supplierMapper.selectById(id) == null) {
            throw exception(SUPPLIER_NOT_EXISTS);
        }
    }

    @Override
    public SupplierDO getSupplier(String id) {
        return supplierMapper.selectById(id);
    }

    @Override
    public PageResult<SupplierDO> getSupplierPage(SupplierPageReqVO pageReqVO) {
        return supplierMapper.selectPage(pageReqVO);
    }




    @Override
    public List<SupplierDO> selectSavedList(Map<String, Object> params) {
        List<SupplierDO> list = supplierMapper.selectSavedList(params);
        return list;
    }

    @Override
    public void insertRecord(List<SupplierDO> insertList) {
        // 插入
        if (null != insertList && insertList.size() > 0) {
            supplierMapper.insertList(insertList);
        }
    }

    @Override
    public void updateRecord(List<SupplierDO> updateList) {
        // 更新
        if (null != updateList && updateList.size() > 0) {
            LocalDateTime today = LocalDateTimeUtil.now();
            for (SupplierDO data : updateList) {
                data.setUpdateTime(today);
            }
            supplierMapper.updateList(updateList);
        }
    }

}